var feed;

var datasetList;

var lastID = 0;
var lastDistance;
var randomInterval;

var busInfo;

busInfo = new Array();
busInfo[0] = {id: 199, distance: 5, awt: 10, seat: 0.8};
busInfo[1] = {id: 179, distance: 10, awt: 20, seat: 0.4};

function PublishBusLocation(){   
//    feed = SAPFeed("http://10.33.247.66:8000", this, 21, "aaa");
    feed = SAPFeed("http://10.33.247.153:8000", this, 21, "aaa");
    
    this.onSAPFeedLogin = function(){
        datasetList = [];        
        datasetList.push(11);
        
        $('#status_bar').append('<p>'+'Logged In'+'</p>');
        
        randomPublish();
    }
    this.onSAPFeedLogout = function(){
        $('#status_bar').append('<p>'+'Logged Out'+'</p>');
    }
    
    this.onSAPFeedError = function(responseReceived){
        $('#status_bar').append('<p>'+responseReceived.code+": "+responseReceived.message+'</p>');
    }

    this.onSAPFeedPublish = function(){
        $('#status_bar').append('<p>'+'Publish Success'+'</p>');
    }
    
    function randomPublish(){

        var busID = lastID;
        
        lastID = (lastID + 1)%2;
        
        busInfo[busID].distance = busInfo[busID].distance - 1;
        busInfo[busID].awt = busInfo[busID].distance * 2 - Math.round(4 * Math.random() - 1);
        busInfo[busID].seat = busInfo[busID].seat + (0.4 * Math.random() - 0.2);
        
        if(busInfo[busID].awt<=0){
            busInfo[busID].awt = 0;
        }
        
        if(busInfo[busID].distance == 0){
            busInfo[busID].awt = 0;
        }
        
        if(busInfo[busID].seat<=0.1){
            busInfo[busID].seat = 0.1;
        }

        
        feed.publish(datasetList, {target: busID, id: busInfo[busID].id,  distance: busInfo[busID].distance, awt: busInfo[busID].awt, seat: getSeatMessage(busInfo[busID].seat)});
//        
//        if(busInfo[1].distance < busInfo[0].distance){
//            var tmpObj = busInfo[1];
//            busInfo[1] = busInfo[0];
//            busInfo[0] = tmpObj;
//            feed.publish(datasetList, {target: 0, id: busInfo[0].id,  distance: busInfo[0].distance, awt: busInfo[0].awt, seat: busInfo[0].seat});
//            feed.publish(datasetList, {target: 1, id: busInfo[1].id,  distance: busInfo[1].distance, awt: busInfo[1].awt, seat: busInfo[1].seat});
//        }
//        
        if(busInfo[0].distance==0){
            var tmpID = busInfo[0].id;
            busInfo[0] = busInfo[1];
            busInfo[1] = {id: tmpID, distance: 10, awt: 20, seat: 0.4};
            feed.publish(datasetList, {target: 0, id: busInfo[0].id,  distance: busInfo[0].distance, awt: busInfo[0].awt, seat: getSeatMessage(busInfo[0].seat)});
            feed.publish(datasetList, {target: 1, id: busInfo[1].id,  distance: busInfo[1].distance, awt: busInfo[1].awt, seat: getSeatMessage(busInfo[1].seat)});            
        }
        
        randomInterval = Math.floor(5000*Math.random());
        setTimeout(function(){
            randomPublish();
        }, randomInterval)
        
    }

    
}
$(document).ready(function(){
    PublishBusLocation();
    $('#stop_btn').click(function(){
        feed.logout();
    });
});

function getSeatMessage(seatCondition){
    if(seatCondition<=0.3){
        return "Almost Empty";
    } else if(seatCondition<=0.9){
        return "Seats Available";
    } else if(seatCondition<=1.2){
        return "Full";
    } else{
        return "Crowded";
    }
}